#include "ExampleElaWindow.h"
#include <QApplication>
#include <QDir>
#include <QHBoxLayout>
#include <DashboardPage.h>
#include <QDebug>


ExampleElaWindow::ExampleElaWindow(QWidget* parent) : ElaWindow(parent), languageMenu(nullptr), toolBar(nullptr), m_btnCamera(nullptr), m_btnDayNight(nullptr), m_mcuStatusText(nullptr), m_statusText(nullptr), dashboardLabel(nullptr), settingsLabel(nullptr)
{
    setupUI();  // 直接设置UI，翻译已在main函数中加载
}

ExampleElaWindow::~ExampleElaWindow()
{
}



void ExampleElaWindow::setupUI()
{
    // 设置窗口标题 - 使用tr()以便翻译
    setWindowTitle(tr("小型机器人"));
    setNavigationBarDisplayMode(ElaNavigationType::Compact);
    setUserInfoCardVisible(false);
    setFocusPolicy(Qt::StrongFocus);
    setAppBarHeight(30);
    setWindowButtonFlags(ElaAppBarType::StayTopButtonHint | ElaAppBarType::MinimizeButtonHint | ElaAppBarType::MaximizeButtonHint | ElaAppBarType::CloseButtonHint | ElaAppBarType::ThemeChangeButtonHint);

    ElaMenuBar* menuBar = new ElaMenuBar(this);
    menuBar->setFixedHeight(30);

    menuBar->addElaIconAction(ElaIconType::GamepadModern, tr("手柄"));

    QWidget* customWidget = new QWidget(this);
    QVBoxLayout* customLayout = new QVBoxLayout(customWidget);
    customLayout->setContentsMargins(0, 0, 0, 0);
    customLayout->addWidget(menuBar);
    customLayout->addStretch();
    this->setCustomWidget(ElaAppBarType::MiddleArea, customWidget);
    this->setCustomWidgetMaximumWidth(500);

    // 工具栏
    toolBar = new ElaToolBar(tr("工具栏"), this);
    toolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
    toolBar->setToolBarSpacing(1);
    toolBar->setIconSize(QSize(25, 25));

    int iconSize = toolBar->height() * 0.6;
    m_btnCamera = new ElaIconButton(ElaIconType::Camera, iconSize, this);
    m_btnCamera->setMaximumWidth(toolBar->height());
    m_btnCamera->setMinimumHeight(toolBar->height());
    m_btnCamera->setToolTip(tr("主屏幕截图"));
    toolBar->addWidget(m_btnCamera);

    m_btnDayNight = new ElaToolButton(this);
    m_btnDayNight->setElaIcon(ElaIconType::Sun);
    m_btnDayNight->setToolTip(tr("相机昼夜切换"));
    toolBar->addWidget(m_btnDayNight);
    toolBar->addSeparator();

    this->addToolBar(Qt::TopToolBarArea, toolBar);

    // 页面内容
    auto dashboardPage = new DashboardPage();
    addPageNode(tr("仪表盘"), dashboardPage, ElaIconType::Alien);

    QWidget* settingsPage = new QWidget(this);
    auto layout_setting = new QVBoxLayout(settingsPage);
    settingsLabel = new QLabel(tr("系统设置页面"), settingsPage);
    settingsLabel->setAlignment(Qt::AlignCenter);
    settingsLabel->setStyleSheet("font-size: 24px; color: #333;");
    layout_setting->addWidget(settingsLabel);
    addPageNode(tr("基本设置"), settingsPage, ElaIconType::Gear);

    // 状态栏
    ElaStatusBar* statusBar = new ElaStatusBar(this);
    statusBar->setFixedHeight(30);
    statusBar->setContentsMargins(10, 0, 0, 0);
    statusBar->setSizeGripEnabled(false);

    // 状态栏项目创建函数
    auto createStatusItem = [this, statusBar](ElaText*& textWidget, ElaIconButton*& iconWidget, const QString& text, ElaIconType::IconName icon) {
        QWidget* container = new QWidget(this);
        QHBoxLayout* layout = new QHBoxLayout(container);
        layout->setContentsMargins(15, 1, 15, 2);
        layout->setSpacing(5);

        int statusIconSize = statusBar->height() * 0.7;
        iconWidget = new ElaIconButton(icon, statusIconSize, container);
        iconWidget->setMaximumWidth(statusBar->height());
        iconWidget->setMaximumHeight(statusBar->height());
        iconWidget->setLightIconColor(ElaThemeColor(ElaThemeType::Light, BasicTextDisable));
        iconWidget->setDarkIconColor(ElaThemeColor(ElaThemeType::Light, BasicTextDisable));
        iconWidget->setLightHoverIconColor(ElaThemeColor(ElaThemeType::Light, BasicTextDisable));
        iconWidget->setDarkHoverIconColor(ElaThemeColor(ElaThemeType::Light, BasicTextDisable));
        iconWidget->setOpacity(0.7);

        textWidget = new ElaText(text, container);
        textWidget->setTextPixelSize(12);
        textWidget->setTextStyle(ElaTextType::NoStyle);
        layout->addWidget(textWidget);
        layout->addWidget(iconWidget);
        layout->setAlignment(Qt::AlignCenter);

        return container;
    };

    ElaIconButton* m_mcuStatusIcon;
    QWidget* mcuStatusItem = createStatusItem(m_mcuStatusText, m_mcuStatusIcon, tr("下位机"), ElaIconType::Robot);

    ElaStatusSeparator* separator_1 = new ElaStatusSeparator(ElaThemeColor(ElaThemeType::Light, BasicTextDisable));
    statusBar->addWidget(separator_1);
    statusBar->addWidget(mcuStatusItem);
    ElaStatusSeparator* separator_2 = new ElaStatusSeparator(ElaThemeColor(ElaThemeType::Light, BasicTextDisable));
    statusBar->addWidget(separator_2);

    this->setStatusBar(statusBar);
}